var SalesUser = Vue.component('salesUser', function (resolve, reject) {
    axios.get('views/salesUser/salesUser.html?timestamp=' + timestamp).then(function (res) {
        resolve({
            template: res.data,
            data(){
                return {
                    needAdminRole:CommonUtils.checkRole('admin'),
                    needAliMgrRole:CommonUtils.checkRole("alipay"),
                    loading: false,
                    tableData: [],
                    disableDetailForm:false,
                    dialogFormVisible: false,
                    form: {},
                    pageInfo:{
                        srchType:"2",
                        currentPage:1,
                        pageSize:10,
                        srch_EQ_isDelete:false,
                        srch_LIKE_name:'',
                        totalNum:10,
                        "srch_JOINEQ_roles.code":'sales',
                        "srch_JOINEQ_aliLifes.id":[]
                    },
                    editForm:{},
                    detailForm:{
                        createBy:{
                            name:''
                        }
                    },
                    srchLifes:[],
                    srchLifeIds:[],
                    showEditWin:false,
                    showDetailWin:false,
                    showResetPwdWin:false,
                    allLifes:[],
                    checkedLifes:[],
                    formLabelWidth: '150px',
                    resetPwdForm:{},
                    tempData:[]
                }
            },
            methods:{

                handleCurrentChange: function(currentPage){
                    this.pageInfo.currentPage = currentPage;
                    this.initData();
                },
                initData:function(){
                    this.loading = true;
                    setTimeout(() => {
                        this.loading = false;
                    }, 2000);

                    if(CommonUtils.checkNull(this.srchLifeIds)){
                        this.pageInfo['srch_JOINEQ_aliLifes.id'] =  ""
                    }else{
                        this.pageInfo['srch_JOINEQ_aliLifes.id'] = this.srchLifeIds;
                    }
                    httpInvoker.get(APIs.auser.initPageData,{
                        params:this.pageInfo,
                        paramsSerializer: params => {
                            return Qs.stringify(params, { indices: false })
                        }
                    }).then(res =>{
                        this.loading = false;
                        // console.log(res.data);
                        if(res.status == 200 && res.data.status == '00'){
                            this.allLifes = res.data.data.allLifes;
                            this.srchLifes = res.data.data.srchLifes;
                            this.tableData = res.data.data.lifeMgrs.content;
                            this.pageInfo.totalNum=res.data.data.lifeMgrs.totalElements;
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },
                openFormWin(event){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.form={};
                    this.checkedLifes=[];
                    this.dialogFormVisible=true;
                },
                resetPwd(event){
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 2000);

                    httpInvoker.post(APIs.auser.resetPwd,this.resetPwdForm).then(res =>{
                        this.loading = false;
                        if(res.status == 200 && res.data.status == '00'){
                            this.showResetPwdWin = false;
                            this.$alert('修改成功', '', {
                                confirmButtonText: '确定'
                            });
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },
                submitForm(event){
                    this.form.relateLifeIds=this.checkedLifes;
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 5000);

                    httpInvoker.post(APIs.auser.addSales,this.form).then(res =>{
                        this.loading = false;
                        if(res.status == 200 && res.data.status == '00'){
                            this.dialogFormVisible = false;
                            this.$alert('创建成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },

                submitEditForm(event){
                    var submitData = {
                        id:this.editForm.id,
                        name:this.editForm.name,
                        account:this.editForm.account,
                        relateLifeIds:this.checkedLifes
                    }
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 5000);

                    httpInvoker.post(APIs.auser.edit,submitData).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.showEditWin = false;
                            this.$alert('编辑成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },
                openDetails(row){
                    httpInvoker.get(APIs.alipay.getRelateLifes,{params:{
                            auid:row.id
                        }}).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.detailForm=row;
                            if(CommonUtils.checkNull(this.detailForm.createBy)){
                                this.detailForm.createBy = {};
                                this.detailForm.createBy.name='';
                            }
                            this.checkedLifes = res.data.data;
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                        this.disableDetailForm = true;
                        this.showDetailWin = true;
                    })

                },
                openResetPwd(row){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.resetPwdForm=row;
                    this.resetPwdForm.pwd='';
                    this.showResetPwdWin=true
                },
                updateUser(row){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.editForm=row;
                    if(CommonUtils.checkNull(this.detailForm.createBy)){
                        this.detailForm.createBy = {};
                        this.detailForm.createBy.name='';
                    }
                    this.checkedLifes = [];
                    if(CommonUtils.checkNotNull(row.aliLifes)){
                        for(vv1=0;vv1<row.aliLifes.length;vv1++){
                            this.checkedLifes.push(row.aliLifes[vv1].id);
                        }
                    }
                    this.showEditWin = true;

                },
                cancelEdit(event){
                    this.showEditWin = false;
                    this.initData();
                },
                deleteUser(row){
                    if(!CommonUtils.checkRole('alipay')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.$confirm('删除'+row.name+'，会作废'+row.name+'下面的所有订单，订单作废后无法恢复！确定要删除吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        httpInvoker.post(APIs.auser.delete,{id:row.id}).then(res =>{
                            if(res.status == 200 && res.data.status == '00'){
                                this.$alert('删除成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.initData();
                                    }

                                });
                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                    }).catch(() => {

                    });
                },
                batchAddLife(lifeId,managerId){
                    var url = APIs.auser.batchAddLifeId;
                    url += "/"+lifeId+"/"+managerId;
                    httpInvoker.post(url,{}).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.$alert('批量增加生活号成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                }
            },
            mounted: function () {
                this.initData();
            },
        })
    })
})